package com.bit.principle.component;

import com.bit.principle.config.BeanConfig;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class BeanLifeComponent implements BeanNameAware, BeanPostProcessor {

    private BeanConfig beanConfig;


    //1.
    public BeanLifeComponent() {
        System.out.println("执行构造方法...");
    }

    //2.
    @Autowired
    public void setBeanConfig(BeanConfig beanConfig) {
        this.beanConfig = beanConfig;
        System.out.println("执行setter方法....");
    }

    //3.
    @Override
    public void setBeanName(String name) {
        System.out.println("执行BeanNameAware.setBeanName方法, name:"+ name);
    }

    //4.
    @PostConstruct
    public void init(){
        System.out.println("执行 init 方法....");
    }

    //6.
//    @Override
//    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        System.out.println("执行postProcessAfterInitialization... ");
//        return bean;
//
//    }
//
//    //5.
//    @Override
//    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//        System.out.println("执行 postProcessBeforeInitialization...");
//        return bean;
//    }

    //7.
    public void use(){
        System.out.println("执行use 方法....");
    }

    //8.
    @PreDestroy
    public void destroy(){
        System.out.println("执行destroy 方法...");
    }
}
